package com.vitabuybuy.tv.ui.tencent.superplay

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.animation.AnimationUtils
import androidx.core.os.bundleOf
import com.library.common.base.BaseActivity
import com.library.common.base.BaseViewModel
import com.tencent.rtmp.TXLiveConstants
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityFillPlaybackBinding
import io.reactivex.functions.Consumer

/**
 * 回放全屏
 */
class FillPlaybackActivity : BaseActivity<BaseViewModel, ActivityFillPlaybackBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = false
    override val layoutId: Int
        get() = R.layout.activity_fill_playback

    private var playback: PlaybackFragment? = null
    private var mCoverUrl: String? = null
    private var mPlayUrl: String? = null
    private var seek: Int? = null

    override fun onGetBundle(bundle: Bundle?) {
        mCoverUrl = bundle?.getString("coverUrl")
        mPlayUrl = bundle?.getString("playUrl")
        seek = bundle?.getInt("seek", 0)
    }

    override fun initView(savedInstanceState: Bundle?) {
        playback = PlaybackFragment()
        playback?.run {
            this.setUrl(mCoverUrl, mPlayUrl, true)
            seek?.let {
                this.seek(it)
            }
            this.showFill = false
            this.mRenderMode = TXLiveConstants.RENDER_MODE_ADJUST_RESOLUTION
            this.mRenderRotation = intent.getIntExtra("RenderRotation", -1)
            supportFragmentManager.beginTransaction()
                .add(R.id.containerFragment, this, "playback").commitAllowingStateLoss()
            this.clickCallback =
                Consumer {
                    if (!it) {
                        (this@FillPlaybackActivity).mBinding.ivBack.visibility = View.GONE
                        (this@FillPlaybackActivity).mBinding.ivBack.startAnimation(
                            AnimationUtils.loadAnimation(
                                this@FillPlaybackActivity,
                                R.anim.anim_alpha_out
                            )
                        )
                    } else {
                        (this@FillPlaybackActivity).mBinding.ivBack.visibility = View.VISIBLE
                        (this@FillPlaybackActivity).mBinding.ivBack.startAnimation(
                            AnimationUtils.loadAnimation(
                                this@FillPlaybackActivity,
                                R.anim.anim_alpha_in
                            )
                        )
                    }
                }
        }
    }

    override fun eventHandling() {
        mBinding.ivBack.setOnClickListener {
            onBackPressed()
        }
    }

    override fun onBackPressed() {
        val i = Intent()
        i.putExtra("seek", playback!!.getSeek())
        setResult(100, i)
        super.onBackPressed()
    }

    companion object {
        fun openActivity(
            mContext: BaseActivity<*, *>,
            coverUrl: String?,
            playUrl: String?,
            seek: Int?
        ) {
            mContext.startForResult(
                bundleOf(
                    Pair("coverUrl", coverUrl),
                    Pair("playUrl", playUrl),
                    Pair("seek", seek)
                ), 0x11, FillPlaybackActivity::class.java
            )
        }
    }
}